﻿using System;
using System.Configuration;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace mojoPortal.Web.RS.SBM.Controls
{
    public partial class BillingListControl : RSBaseUserControl
    {

        protected void Page_Load(object sender, EventArgs e)
        {
        }


        public bool LoadBillingList(int companyid)
        {
            bool bHasValue = true;

            SqlCommand myCommand;
            SqlParameter param;
            SqlConnection myConnection;

            String sqlSELECT = "select c.contactid, c.firstname, c.lastname, c.Email, c.Mobile, c.Phone, i.WorkOrderId,i.invoiceid, i.locationid, i.balancedue_cf, i.billingcontactid from rs_tblinvoice i inner join rs_tblContact c on i.BillingContactId=c.ContactId where i.companyid=@companyid and i.BalanceDue_CF > 0 order by c.lastname, c.firstname";

            myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString());
            myCommand = new SqlCommand(sqlSELECT, myConnection);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@companyid";
            param.Value = companyid;
            myCommand.Parameters.Add(param);

            SqlDataAdapter adapter = new SqlDataAdapter(myCommand);
            myConnection.Open();
            DataSet ds = new DataSet();
            adapter.Fill(ds);
            Session["DATATABLE"] = ds.Tables[0];
            gvBillingList.DataSource = ds;
            gvBillingList.DataBind();
            myConnection.Close();

            //lblCount.Text = gvBillingInformation.Rows.Count.ToString();
            if (gvBillingList.Rows.Count == 0)
            {
                bHasValue = false;
                return bHasValue;
            }
            return bHasValue;
        }
        private string ConvertSortDirectionToSql(SortDirection sortDirection)
        {
            string newSortDirection = String.Empty;

            switch (sortDirection)
            {
                case SortDirection.Ascending:
                    newSortDirection = "ASC";
                    break;

                case SortDirection.Descending:
                    newSortDirection = "DESC";
                    break;
            }
            return newSortDirection;
        }

        protected void gvBillingList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvBillingList.PageIndex = e.NewPageIndex;
            gvBillingList.DataBind();
        }

        protected void gvBillingList_Sorting(object sender, GridViewSortEventArgs e)
        {
            DataTable dataTable = Session["DATATABLE"] as DataTable;
            if (dataTable != null)
            {
                DataView dataView = new DataView(dataTable);
                dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);
                gvBillingList.DataSource = dataView;
                gvBillingList.DataBind();
            }
        }

    }
}